package com.apexsoft.assets;

import com.apexsoft.assets.base.ApplicationStartedEventListener;
import com.apexsoft.live.utils.DefaultEncryptor;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.DefaultApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.context.config.annotation.RefreshScope;

//@EnableDiscoveryClient    // springcloud
@SpringBootApplication
@RefreshScope
@ServletComponentScan
@EnableCaching
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String... args) {
        ApplicationArguments arguments = new DefaultApplicationArguments(args);
        StringEncryptor jasypt = new DefaultEncryptor();
        if (arguments.containsOption("encrypt")) {
            System.out.println("----------------------------------------------------");
            for (String text : arguments.getNonOptionArgs()) {
                try {
                    System.out.println(text + ": ENC(" + jasypt.encrypt(text)+")");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else if(arguments.containsOption("decrypt")) {
            System.out.println("----------------------------------------------------");
            for (String text : arguments.getNonOptionArgs()) {
                try {
                    System.out.println(text + ": " + jasypt.decrypt(text));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else {
            SpringApplication springApplication = new SpringApplication(DemoApplication.class);
            springApplication.addListeners(new ApplicationStartedEventListener());
            springApplication.run(args);
        }
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }
}